package com.xinsoft.controller.mes;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xinsoft.aop.ano.SysLog;
import com.xinsoft.common.BusinessType;
import com.xinsoft.common.Response;
import com.xinsoft.constant.Const;
import com.xinsoft.entity.dto.MesProducePlanDTO;
import com.xinsoft.entity.dto.MesProducePlantParam;
import com.xinsoft.entity.dto.MesProductionExecutionFeedbackParam;
import com.xinsoft.entity.dto.QuaCheckPlanParam;
import com.xinsoft.entity.po.MesProducePlan;
import com.xinsoft.entity.po.QuaCheckPlan;
import com.xinsoft.entity.po.SysConfig;
import com.xinsoft.service.*;
import com.xinsoft.utils.TokenUtil;
import com.xinsoft.validator.ValidatorUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * @ClassName MesProducePlanController
 * @Description 生产计划
 * @Author Li9527
 * @Date 22023-7-24 14:10:36
 * @Version 1.0
 */
@RestController
@RequestMapping("/mes/produce")
@Api(value = "MesProducePlanController", tags = {"生产计划"})
public class MesProducePlanController {

    @Autowired
    private MesProducePlanService producePlanService;

    @Autowired
    private SysConfigService configService;

    @Autowired
    SysUserService userService;

    @Autowired
    QuaCheckPlanService checkPlanService;

    @Resource
    public QuaCheckPlanService quaCheckPlanService;

    @Resource
    public SysDocumentNumService sysDocumentNumService;
    /**
     * @return com.xinsoft.common.Response<?>
     * @Author Li9527
     * @Description 生产计划-新增
     * @Date 2023/5/17 9:57
     * @params dept
     **/
    @SysLog(title = "生产计划-新增", businessType = BusinessType.INSERT)
    @ApiOperation("新增")
    @PostMapping("/add")
    @Transactional(rollbackFor = Exception.class)
    public Response<?> add(@RequestBody MesProducePlanDTO planDTO) {
        // 参数校验
        ValidatorUtils.validateEntity(planDTO);

        sysDocumentNumService.incrementLastSerialByDocumentId(Const.DOCUMENT_ID.MES_PRODUCE_PLAN_DOCUMENT);
        // 默认已审核
        planDTO.setAuditId(Const.ADMIN_UID);
        planDTO.setAuditStatus(Integer.valueOf(configService.getOne(new QueryWrapper<SysConfig>().eq("param_key", "MES_CRAFT_DEFAULT_AUDIT_STATUS")).getParamValue()));
        producePlanService.create(planDTO);
        return Response.succeed();
    }

    /**
     * @return com.xinsoft.common.Response<?>
     * @Author Li9527
     * @Description 更新
     * @Date 2023/5/17 9:57
     * @params dept
     **/
    @ApiOperation("变更")
    @SysLog(title = "生产计划-编辑", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @Transactional(rollbackFor = Exception.class)
    public Response<?> edit(@RequestBody MesProducePlanDTO planDTO) {
        // 参数校验
        ValidatorUtils.validateEntity(planDTO);
        MesProducePlan producePlanDO = producePlanService.getById(planDTO.getId());
        if (Const.AUDIT_STATUS_NORMAL.equals(producePlanDO.getAuditStatus())){
            return Response.fail("审核状态单据不能修改!");
        }
        // 默认已审核
        planDTO.setAuditId(Const.ADMIN_UID);
        planDTO.setAuditStatus(Integer.valueOf(configService.getOne(new QueryWrapper<SysConfig>().eq("param_key", "MES_PROCESS_DEFAULT_AUDIT_STATUS")).getParamValue()));
        producePlanService.edit(planDTO);
        return Response.succeed();
    }

    /**
     * @return com.xinsoft.common.Response<?>
     * @Author Li9527
     * @Description 获取详情
     * @Date 2023/5/17 9:57
     * @params id
     **/
    @ApiOperation("获取详情")
    @GetMapping("/detail")
    public Response<?> detail(@RequestParam(value = "id", defaultValue = "", required = true) Integer id) {
        MesProducePlanDTO result = producePlanService.getInfoById(id);
        //检验方案
        QuaCheckPlan quaCheckPlan = quaCheckPlanService.getById(result.getInspectionScheme());
        if(quaCheckPlan!=null){
            result.setInspectionSchemeName(quaCheckPlan.getCheckPlanName());
        }
        return Response.succeed(result);
    }

    /**
     * @return com.xinsoft.common.Response<?>
     * @Author Li9527
     * @Description 删除
     * @Date 2023/5/12 9:20
     * @params ids
     **/
    @ApiOperation("删除")
    @SysLog(title = "生产计划-删除", businessType = BusinessType.DELETE)
    @PostMapping("/del")
    public Response<?> del(@RequestBody List<Integer> ids) {
        for (int i = 0; i < ids.size(); i++) {
            MesProducePlan producePlan = producePlanService.getById(ids.get(i));
            if (producePlan != null && producePlan.getAuditStatus().equals(Const.AUDIT_STATUS_NORMAL)) {
                return Response.fail("存在已审核生产计划，不允许删除");
            }
        }
        producePlanService.removeByIds(ids);
        return Response.succeed();
    }

    /**
     * @return com.xinsoft.common.Response<?>
     * @Author Li9527
     * @Description 批量审核
     * @Date 2023/5/12 9:20
     * @params ids
     **/
    @ApiOperation("生产计划-批量审核")
    @SysLog(title = "生产计划-批量审核", businessType = BusinessType.UPDATE)
    @PostMapping("/audit")
    public Response<?> auditBacth(@RequestBody List<Integer> ids) {
        Integer auditId = TokenUtil.getUser().getId();
        List<MesProducePlan> list = producePlanService.list(new QueryWrapper<MesProducePlan>().in("id", ids));
        for (int i = 0; i < list.size(); i++) {
            list.get(i).setAuditStatus(Const.AUDIT_STATUS_NORMAL);
            list.get(i).setAuditId(auditId);
        }
        producePlanService.updateBatchById(list);
        return Response.succeed();
    }

    /**
     * @return com.xinsoft.common.Response<?>
     * @Author Li9527
     * @Description 批量反审核
     * @Date 2023/5/12 9:20
     * @params ids
     **/
    @ApiOperation("生产计划-批量反审核")
    @SysLog(title = "生产计划-批量反审核", businessType = BusinessType.UPDATE)
    @PostMapping("/deAudit")
    public Response<?> unAuditBatch(@RequestBody List<Integer> ids) {
        List<MesProducePlan> list = producePlanService.list(new QueryWrapper<MesProducePlan>().in("id", ids));
        Integer auditId = TokenUtil.getUser().getId();
        for (int i = 0; i < list.size(); i++) {
            list.get(i).setAuditStatus(Const.AUDIT_STATUS_WAIT);
            list.get(i).setAuditId(auditId);
        }
        producePlanService.updateBatchById(list);
        return Response.succeed();
    }

    /**
     * @return com.xinsoft.common.Response<?>
     * @Author Li9527
     * @Description 生产计划-分页
     * @Date 2023/5/18 10:51
     * @params param
     **/
    @PostMapping("/pageList")
    public Response<?> pageList(@Valid @RequestBody MesProducePlantParam param) {
        return Response.succeed(producePlanService.findPageListByParam(param));
    }

    @PostMapping("/findTotalByParam")
    public Response<?> findTotalByParam(@Valid @RequestBody MesProducePlantParam param) {
        return Response.succeed(producePlanService.findTotalByParam(param));
    }


    @PostMapping("/queryForAnalysis")
    public Response<?> queryForAnalysis(@Valid @RequestBody MesProducePlantParam param) {
        return Response.succeed(producePlanService.queryForAnalysis(param));
    }

    /**
     * @return com.xinsoft.common.Response<?>
     * @Author Li9527
     * @Description 检验方案-分页
     * @Date 2023/5/18 10:51
     * @params param
     **/
    @PostMapping("/checkPlan/pageList")
    public Response<?> pageList(@Valid @RequestBody QuaCheckPlanParam param) {
        return Response.succeed(checkPlanService.findPageListByParam(param));
    }

    /**
     * 下达生产计划
     * @return com.xinsoft.common.Response<?>
     * @Author Li9527
     * @Description 下达生产计划
     * @Date 2023/5/18 10:51
     * @params param
     **/
    @Transactional(rollbackFor = Exception.class)
    @PostMapping(value = "/issuedPlans")
    public Response<?>  issuedPlans(@RequestBody List<Integer> ids) {
        producePlanService.issuedPlans(ids);
        return Response.succeed();
    }

    /**
     * 反下达
     * @return com.xinsoft.common.Response<?>
     * @Author Li9527
     * @Description 反下达生产计划
     * @Date 2023/5/18 10:51
     * @params param
     **/
    @Transactional(rollbackFor = Exception.class)
    @PostMapping(value = "/reverseIssuedPlan")
    public Response<?>  reverseIssuedPlan(@RequestBody List<Integer> ids) {
        producePlanService.reverseIssuedPlan(ids);
        return Response.succeed();
    }

    /**
     * 挂起
     * @return com.xinsoft.common.Response<?>
     * @Author Li9527
     * @Description 反下达生产计划
     * @Date 2023/5/18 10:51
     * @params param
     **/
    @Transactional(rollbackFor = Exception.class)
    @PostMapping(value = "/putUp")
    public Response<?>  putUp(@RequestBody List<Integer> ids) {
        producePlanService.putUp(ids);
        return Response.succeed();
    }

    /**
     * 反挂起
     * @return com.xinsoft.common.Response<?>
     * @Author Li9527
     * @Description 反挂起生产计划
     * @Date 2023/5/18 10:51
     * @params param
     **/
    @Transactional(rollbackFor = Exception.class)
    @PostMapping(value = "/reversePutUp")
    public Response<?>  reversePutUp(@RequestBody List<Integer> ids) {
        producePlanService.reversePutUp(ids);
        return Response.succeed();
    }

    /**
     * 结案
     * @return com.xinsoft.common.Response<?>
     * @Author Li9527
     * @Description 反结案生产计划
     * @Date 2023/5/18 10:51
     * @params param
     **/
    @Transactional(rollbackFor = Exception.class)
    @PostMapping(value = "/close")
    public Response<?>  close(@RequestBody List<Integer> ids) {
        producePlanService.close(ids);
        return Response.succeed();
    }

    /**
     * @return com.xinsoft.common.Response<?>
     * @Author Li9527
     * @Description 反结案生产计划
     * @Date 2023/5/18 10:51
     * @params param
     **/
    @Transactional(rollbackFor = Exception.class)
    @PostMapping(value = "/reverseClose")
    public Response<?>  reverseClose(@RequestBody List<Integer> ids) {
        producePlanService.reverseClose(ids);
        return Response.succeed();
    }

    /**
     * @return com.xinsoft.common.Response<?>
     * @Author Li9527
     * @Description 生产执行反馈
     * @Date 2023/5/18 10:51
     * @params param
     **/
    @Transactional(rollbackFor = Exception.class)
    @PostMapping(value = "/productionExecutionFeedback")
    public Response<?>  reverseClose(@Valid @RequestBody MesProductionExecutionFeedbackParam param) {
        return Response.succeed(producePlanService.productionExecutionFeedback(param));
    }

}
